package com.ykzhujiang.lovespirit.labor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ykzhujiang.lovespirit.util.Utility;

public class TongjiLabor extends Labor {

	public int Fetch(int page_num) {

		int count = 0;
		String url = url_text;
		while (count++ < page_num && url != null) {
			String text = Utility.GetURLText(url, connect_times);
			if (text == null) {
				return -1;
			}
			Pattern p = Pattern.compile(thread_pattern);
			Matcher m = p.matcher(text);
			while (m.find()) {
				String raw = thread_root + m.group(1);
				if (!files.contains(raw)) {
					files.add(raw);
				}
			}
			p = Pattern.compile(page_pattern);
			m = p.matcher(text);
			for (int i = 0; i < 2; i++) {
				if (m.find()) {
					url = url_root + m.group(0);
				} else {
					url = null;
				}
			}
		}
		return 0;

	}

	public void SetBoard(String board) {
		this.board = board;
		// thread_pattern = "bbstcon\\.php\\?board=" + board + "&gid=.*?(?=\")";
		page_pattern = "bbsdoc\\.php\\?board=" + board + suffix
				+ "&page=.*?(?=\")";
		url_text = "http://bbs.tongji.edu.cn/bbsdoc.php?board=" + board
				+ suffix;
		url_root = "http://bbs.tongji.edu.cn";
		thread_root = "http://bbs.tongji.edu.cn/bbstcon.php?board=" + board
				+ "&gid=";
	}

	public String GetBorad() {
		return board;
	}

	private String board = "PieBridge";

	private String suffix = "&ftype=6";

	// private String thread_pattern = "bbstcon\\.php\\?board=" + board
	// + "&gid=.*?(?=\")";

	private String thread_pattern = "c\\.o\\(([\\d]+).*?\\);";

	private String page_pattern = "bbsdoc\\.php\\?board=" + board + suffix
			+ "&page=.*?(?=\")";

	private String url_text = "http://bbs.tongji.edu.cn/bbsdoc.php?board="
			+ board + suffix;

	private String url_root = "http://bbs.tongji.edu.cn/";

	private String thread_root = "http://bbs.tongji.edu.cn/bbstcon.php?board="
			+ board + "&gid=";
}
